var form;
var layer;
//页面初始化时加载Layui框架
layui.use(['form', 'layer', 'laydate'], function () {
    form = layui.form();
    layer = layui.layer;

    initCustomerTree();

    var isClick = false;
    $("#customerSel").click(function () {
        isClick = true;
        $('#menuContent').toggle(true);
        return false;
    });
    showOrHideTree(isClick);
    // 人员选择不能点击回车
    $("#customerSel").keydown(function (e) {
        if (e.which == 8) {
            return false;
        }
    });
});

/**
 * 显示和隐藏树
 * @param isClick 是否点击加载出人员树
 */
function showOrHideTree(isClick) {
    var flag = true;
    $("#menuContent").mouseenter(function () {
        //显示代码
        $('#menuContent').toggle(true);
        flag = false;
    });
    $("#menuContent").mouseleave(function () {
        $("#menuContent").toggle(false);
    });
    $("#userSel").mouseleave(function () {
        if (flag && isClick) {
            //显示代码
            $('#menuContent').toggle(true);
        } else {
            //隐藏代码
            $('#menuContent').toggle(false);
        }
    });
}

/**
 * 获取群组的树结构
 */
function initCustomerTree() {
    // 设置树样式
    // 开始隐藏
    $("#menuContent").hide();
    // 组织机构树参数设置
    var setting = {
        check: {
            enable: true,
            chkStyle: "radio",
            radioType: "level"
        },
        view: {
            dblClickExpand: false
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onClick: onTreeNodeClick,
            onCheck: zTreeOnCheck
        }
    };
    $.ajax({
        url: basePath + "customer/selectCustomer.action",
        type: 'post',
        dataType: 'json',
        success: function (data) {
            var treeObj = $.fn.zTree.init($("#customerTree"), setting, data);
            // 默认展示第一个客户所在的组
            var rootNodesArr = treeObj.getNodes();
            for (var j = 0; j < rootNodesArr.length; j++) {
                var nodes = treeObj.transformToArray(rootNodesArr[j]);
                var isExpand = false;
                for (var i = 0; i < nodes.length; i++) {
                    if (nodes[i].id.indexOf("cusid_") == 0) {
                        var nodesArr = treeObj.getNodesByParam("id", nodes[i].id, null);
                        var tempNode = nodesArr[0];
                        while (tempNode.getParentNode() != null) {
                            treeObj.expandNode(tempNode.getParentNode(), true);
                            tempNode = tempNode.getParentNode();
                        }
                        isExpand = true;
                        break;
                    }
                }
                if (isExpand) {
                    break;
                }
            }
            // 选择客户
            getCustomer();
            //初始化客户
            initCustomer();
        }
    });
}

/**
 * 得到客户
 */
function getCustomer() {
    var customerId = $("#customerId").val();
    var zTree = $.fn.zTree.getZTreeObj("customerTree");
    var nodes = zTree.transformToArray(zTree.getNodes());
    for (var i = 0; i < nodes.length; i++) {
        var customerName = nodes[i].name;
        var uId = nodes[i].id;
        if (uId == "cusid_" + customerId) {
            zTree.selectNode(nodes[i]);
            $("#customerSel").val(customerName);
        }
    }
}
/**
 * 群组树调用点击事件
 */
function onTreeNodeClick(e, treeId, treeNode) {
    var zTree = $.fn.zTree.getZTreeObj("customerTree");
    var nodes = zTree.getSelectedNodes();
    if (nodes.length > 0) {
        var nodeId = nodes[nodes.length - 1].id;
        if (!treeNode.isParent && nodeId.substring(6, nodeId.length) != 0) {
            $("#customerId").val(nodeId.substring(6, nodeId.length));
            $("#customerSel").val(recursionNodeName(nodes[nodes.length - 1], ""));
            $("#menuContent").hide();
            zTree.checkNode(nodes[nodes.length - 1], true, false);
        }
    }
}

/**
 * 节点的选中事件
 */
function zTreeOnCheck(e, treeId, treeNode) {
    var nodeId = treeNode.id;
    if (!treeNode.isParent && nodeId.substring(6, nodeId.length) != 0) {
        $("#customerId").val(nodeId.substring(6, nodeId.length));
        $("#customerSel").val(recursionNodeName(treeNode, ""));
        $("#menuContent").hide();
    }

}
/**
 * 初始化客户
 */
function initCustomer() {
    var zTree = $.fn.zTree.getZTreeObj("customerTree");
    var customerId = $("#customerId").val();
    var currentNode = zTree.getNodesByParam("id", "cusid_" + customerId, null);
    if (currentNode.length > 0) {
        zTree.checkNode(currentNode[currentNode.length - 1], true, false);
        zTree.updateNode(currentNode[currentNode.length - 1]);
        $("#customerSel").val(currentNode[currentNode.length - 1].name);
        $("#customerSel").val(recursionNodeName(currentNode[currentNode.length - 1], ""));
    }

}

/**
 * 递归出节点的名称
 */
function recursionNodeName(treeNode, reName) {
    if (reName == "") {
        reName = treeNode.name;
    } else {
        reName = treeNode.name + ">" + reName;
    }
    var pNode = treeNode.getParentNode();
    if (pNode != null) {
        return recursionNodeName(pNode, reName);
    } else {
        return reName;
    }
}

/**
 * 清空选中客户
 */
function clearCustomer() {
    var zTree = $.fn.zTree.getZTreeObj("customerTree");
    var nodes = zTree.getCheckedNodes(true);
    for (var j = 0; j < nodes.length; j++) {
        zTree.checkNode(nodes[j], false, false);
        zTree.updateNode(nodes[j]);
    }
    $("#customerSel").val("");
    $("#customerId").val("");
}